package com.mission.schedule.activity;

import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;

public class AboutUsActivity extends BaseActivity {

    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;
    @ViewResId(id = R.id.headtitle_tv)
    private TextView headtitle_tv;
    @ViewResId(id = R.id.version_name)
    private TextView version_name;
    @ViewResId(id = R.id.openqq)
    private TextView openqq;
    @ViewResId(id = R.id.openurl)
    private TextView openurl;
    @ViewResId(id = R.id.open_weixin)
    private TextView open_weixin;

    Context context;

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AboutUsActivity.this.finish();
            }
        });
        openqq.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkApkExist(AboutUsActivity.this, "com.tencent.mobileqq")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=3098997562&version=1")));
                } else {
                    Toast.makeText(AboutUsActivity.this, "本机未安装QQ应用", Toast.LENGTH_SHORT).show();
                }
            }
        });

        openurl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http://www.keytimeapp.com");
                intent.setData(content_url);
                startActivity(intent);
            }
        });
        open_weixin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkApkExist(AboutUsActivity.this, "com.tencent.mm")) {
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    cm.setText("Akeytime");
                    Toast.makeText(AboutUsActivity.this, "已复制微信号", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
                    intent.setAction(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setComponent(cmp);
                    startActivity(intent);
                } else {
                    Toast.makeText(AboutUsActivity.this, "本机未安装微信", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_aboutus);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        headtitle_tv.setText("关于我们");
        version_name.setText(getVersion());
    }

    @Override
    protected void setAdapter() {

    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return this.getString(R.string.app_name) + " V " + version;
        } catch (Exception e) {
            e.printStackTrace();
            return this.getString(R.string.app_name);
        }
    }

    public boolean checkApkExist(Context context, String packageName) {
        if (packageName == null || "".equals(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
}
